************************************************************************* 
*																		*
* 		REPLICATION ARCHIVE FOR GOODMAN (FORTHCOMING CPS) 				*
*																		*
*  NOTES: 																*
*																		*
*	1. you must change the file path below to your working directory	*
*   2. this requires you to install coefplot, esttab, and grc1leg		*
*																		*
*  OUTPUTS:																*
*	1. summary statistics												*
*	2. figures (main and appendix)										*
*	3. regression tables for party/brexit models						*
*	4. coding reports for the objection analysis						*
*																		*
************************************************************************* 



cd "___SET YOUR PATH HERE__"



set matsize 800

*************************
* clean and combine data
* produce balance tables
*************************


* US

use working_data_us.dta, clear
keep gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelamerican gc_speakenglish n_exp female age college region pid3 race
gen race1 = 0
replace race1 = 1 if race==2
gen race2 = 0
replace race2 = 1 if race>2
gen right = pid3 == 2
gen gop = pid3==2
gen democrat = pid3==1
gen other = pid3>2
gen imm_treatment = 1 if n_exp==3
replace imm_treatment = 0 if n_exp==4
tabulate region, generate(US_rdummy)
decode region, generate(region_str)
levelsof region_str, local(NAMES)
foreach name of local NAMES {
    gen reg_`name' = (region_str == "`name'")
}
asdoc sum imm_treatment gc_* female age college right reg_* if n_exp==3 | n_exp==4, label replace save(sumstats_us.doc)
balancetable imm_treatment female age college right reg_* using "balance_table_us.xlsx", ctitles("Control group" "Treatment group" "Difference" "Standardized Difference") pvalues stddiff starlevels(* 0.05 ** 0.01) replace
gen country = "US"
save us.dta, replace


* UK

use working_data_uk.dta, clear
keep gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelamerican gc_speakenglish n_exp female age college region v_98 v_101 v_99 v_78
gen race1 = 0
replace race1 = 1 if v_78==4 | v_78==5 | v_78==6
gen race2 = 0
replace race2 = 1 if v_78>6
gen right = (v_98 == 2 | v_98==8)
gen brexit = v_101 == 2
gen remain = v_101 == 3
gen abstain = v_101 == 4
gen ingovt = (v_98==2 | v_99==63 | v_99==64 | v_99==65)
gen tory = v_98 == 2
gen labour = v_98 == 3
gen ukip = v_98 == 8
gen none = tory==0 & labour == 0 & ukip == 0
gen imm_treatment = 1 if n_exp==3
replace imm_treatment = 0 if n_exp==4
tabulate region, generate(UK_rdummy)
decode region, generate(region_str)
replace region_str = subinstr(region_str," ","",.)
levelsof region_str, local(NAMES)
foreach name of local NAMES {
    gen reg_`name' = (region_str == "`name'")
}
asdoc sum imm_treatment gc_* female age college right brexit reg_* if n_exp==3 | n_exp==4, label replace save(sumstats_uk.doc)
balancetable imm_treatment female age college right brexit reg_* using "balance_table_uk.xlsx", ctitles("Control group" "Treatment group" "Difference" "Standardized Difference") pvalues stddiff starlevels(* 0.05 ** 0.01) replace
gen country = "UK"
save uk.dta, replace


* DE

use working_data_de.dta, clear
keep gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelamerican gc_speakenglish n_exp female age college region party v_84 v_85
tostring v_85, replace force
gen race1 = 0
replace race1 = 1 if v_84==2
replace race1=0 if v_85=="Amerika"
replace race1=0 if v_85=="Australien"
replace race1=0 if v_85=="austria"
replace race1=0 if v_85=="Belarus"
replace race1=0 if v_85=="Belgien"
replace race1=0 if v_85=="Bulgarien"
replace race1=0 if v_85=="CSSR"
replace race1=0 if v_85=="Dänemark"
replace race1=0 if v_85=="DDR"
replace race1=0 if v_85=="deitschland"
replace race1=0 if v_85=="Deutsches Reich"
replace race1=0 if v_85=="deutschland"
replace race1=0 if v_85=="england"
replace race1=0 if v_85=="England"
replace race1=0 if v_85=="Frankreich"
replace race1=0 if v_85=="grik"
replace race1=0 if v_85=="Grossbritannien"
replace race1=0 if v_85=="italien"
replace race1=0 if v_85=="Italien"
replace race1=0 if v_85=="Italien mit Mazedonischem Pass"
replace race1=0 if v_85=="Jugoslawien"
replace race1=0 if v_85=="Kanada"
replace race1=0 if v_85=="Kosovo"
replace race1=0 if v_85=="Kroatien"
replace race1=0 if v_85=="Litauen"
replace race1=0 if v_85=="Luxemburg"
replace race1=0 if v_85=="Mazedonien"
replace race1=0 if v_85=="Moldau"
replace race1=0 if v_85=="Moldawien"
replace race1=0 if v_85=="NHIUZTRRF"
replace race1=0 if v_85=="niederlande"
replace race1=0 if v_85=="Niederlande"
replace race1=0 if v_85=="niederlanden"
replace race1=0 if v_85=="Niederlanden"
replace race1=0 if v_85=="Oberschlesien"
replace race1=0 if v_85=="österreich"
replace race1=0 if v_85=="Österreich"
replace race1=0 if v_85=="pl"
replace race1=0 if v_85=="polen"
replace race1=0 if v_85=="Polen"
replace race1=0 if v_85=="Romania"
replace race1=0 if v_85=="Roumanien"
replace race1=0 if v_85=="Rumaenien"
replace race1=0 if v_85=="Rumänien"
replace race1=0 if v_85=="Rumanien"
replace race1=0 if v_85=="Russ"
replace race1=0 if v_85=="Russische Föderation"
replace race1=0 if v_85=="russland"
replace race1=0 if v_85=="Russland"
replace race1=0 if v_85=="rußland"
replace race1=0 if v_85=="Rußland"
replace race1=0 if v_85=="Schottland"
replace race1=0 if v_85=="Schweden"
replace race1=0 if v_85=="Schweiz"
replace race1=0 if v_85=="Slowakei"
replace race1=0 if v_85=="spanien"
replace race1=0 if v_85=="Spanien"
replace race1=0 if v_85=="uk"
replace race1=0 if v_85=="ukraine"
replace race1=0 if v_85=="Ukraine"
replace race1=0 if v_85=="Ungarn"
replace race1=0 if v_85=="USA"
replace race1=0 if v_85=="Üsterreich"
replace race1=0 if v_85=="Weißrussland"	
gen race2 = 0
replace race2 = 1 if v_84==2
replace race2=0 if v_85=="Ägypten"
replace race2=0 if v_85=="Argentinien"
replace race2=0 if v_85=="Armenien"
replace race2=0 if v_85=="Aserbaidschan"
replace race2=0 if v_85=="Asia"
replace race2=0 if v_85=="Bangladesh"
replace race2=0 if v_85=="brasilien"
replace race2=0 if v_85=="Brasilien"
replace race2=0 if v_85=="guinea"
replace race2=0 if v_85=="Indien"
replace race2=0 if v_85=="Indonesien"
replace race2=0 if v_85=="Iran"
replace race2=0 if v_85=="Israel"
replace race2=0 if v_85=="Japan"
replace race2=0 if v_85=="Kamerun"
replace race2=0 if v_85=="Kasachstan"
replace race2=0 if v_85=="Kasschstan"
replace race2=0 if v_85=="Kirgistan"
replace race2=0 if v_85=="Madagaskar"
replace race2=0 if v_85=="Malaysia"
replace race2=0 if v_85=="möchte nicht sagen"
replace race2=0 if v_85=="nicht wichtig"
replace race2=0 if v_85=="Nicht wichtig"
replace race2=0 if v_85=="pakistan"
replace race2=0 if v_85=="Philippinen"
replace race2=0 if v_85=="Sierra Leone"
replace race2=0 if v_85=="Spielt keine Rolle"
replace race2=0 if v_85=="Sudan"
replace race2=0 if v_85=="syrien"
replace race2=0 if v_85=="Syrien"
replace race2=0 if v_85=="Thailand"
replace race2=0 if v_85=="Togo"
replace race2=0 if v_85=="tschechien"
replace race2=0 if v_85=="Tschechische Republik"
replace race2=0 if v_85=="türkei"
replace race2=0 if v_85=="Türkei"
replace race2=0 if v_85=="Unkekannt"
replace race2=0 if v_85=="Usbekistan"
replace race2=0 if v_85=="Vietnam"
replace race2=0 if v_85=="xxx"
gen right = (party == 2 | party == 7 | party == 9)
gen ingovt = (party == 2 | party == 3)
gen imm_treatment = 1 if n_exp==3
replace imm_treatment = 0 if n_exp==4
tabulate region, generate(DE_rdummy)
decode region, generate(region_str)
replace region_str = subinstr(region_str,"-","",.)
levelsof region_str, local(NAMES)
foreach name of local NAMES {
    gen reg_`name' = (region_str == "`name'")
}
asdoc sum imm_treatment gc_* female age college right ingovt reg_* if n_exp==3 | n_exp==4, label replace save(sumstats_de.doc)
balancetable imm_treatment female age college right ingovt reg_* using "balance_table_de.xlsx", ctitles("Control group" "Treatment group" "Difference" "Standardized Difference") pvalues stddiff starlevels(* 0.05 ** 0.01) replace
gen country = "DE"
save de.dta, replace

use us.dta, clear
append using uk.dta
append using de.dta
save combined.dta, replace
rm us.dta
rm uk.dta
rm de.dta

rename gc_feelamerican gc_feelX
rename gc_speakenglish gc_speakX

label var gc_alwaysvote "Always Votes"
label var gc_obeylaws "Obeys the Law"
label var gc_keepwatch "Watches the Gov't"
label var gc_associations "Joins Associations"
label var gc_helppeople "Helps People"
label var gc_others "Understands Others"
label var gc_frienddiff "Different Friends"
label var gc_havepatience "Is Patient"
label var gc_supportgov "Support Govt"
label var gc_protest "Protests"
label var gc_understandpol "Understands Politics"
label var gc_diversity "Supports Diversity"
label var gc_feelX "Feels X"
label var gc_speakX "Speaks X"



************************************
* GENERATE MEAN-CENTERED COVARIATES
************************************

local countries = "US UK DE"
local ivs = "female age college race1 race2"

foreach country of local countries {
	
	unab regions : `ivs' `country'_rdummy*
	
	foreach tocenter of local regions {
		summarize `tocenter' if country==`"`country'"'
		generate cent_`country'_`tocenter' = `tocenter' - r(mean)
	}

}



**********************
* BASE ANALYSES
**********************

local dvs "gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelX gc_speakX"
local countries = "US UK DE"

estimates drop _all
graph drop _all


* create label graphs

twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "Behavior", size(vhuge)) name(behavior, replace)
twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "Liberal" "Values", size(vhuge)) name(beliefs, replace)
twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "National" "Belonging", size(vhuge)) name(belonging, replace)


* run regressions

foreach var of local dvs {

	foreach country of local countries {
			
		reg `var' ib4.n_exp##c.cent_`country'* if country==`"`country'"' & n_exp != 1 & n_exp != 2, robust
		eststo i_`var'_`country'
		
	}
	
}


* coefficient plots

local i = 1
foreach var of local dvs {
	
	coefplot i_`var'*, keep(3.n_exp) name(i_`var', replace) xline(0) ciopts(lwidth(medium)) title("`: var label `var''") scheme(tpepbw) ///
		p1(label(US) msymbol(D)) p2(label(UK) msymbol(O)) p3(label(Germany) msymbol(T)) legend(rows(1)) coeflabels(3.n_exp = " ") 
		
	local i = `i' + 1
	
}


* combine them

grc1leg behavior i_gc_alwaysvote i_gc_keepwatch i_gc_protest i_gc_understandpol  i_gc_associations ///
	beliefs i_gc_others i_gc_frienddiff i_gc_havepatience i_gc_diversity i_gc_helppeople ///
	belonging i_gc_obeylaws    i_gc_supportgov   i_gc_feelX i_gc_speakX, xcommon title("Immigration Treatment") rows(3) legendfrom(i_gc_alwaysvote)
graph export figure1.png, as(png) replace



*************************
* ESTIMATES BY PARTIES
*************************


estimates drop _all

local dvs "gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelX gc_speakX"

preserve 


* run regressions

foreach var of local dvs {

		
		reg `var' ib4.n_exp##c.cent_US* if country=="US" & n_exp != 1 & n_exp != 2, robust
		eststo i_`var'_US
		estadd local resp_sample "All"

		reg `var' ib4.n_exp##c.cent_US* if country=="US" & n_exp != 1 & n_exp != 2 & gop==1, robust
		eststo i_gop_`var'_US
		estadd local resp_sample "Republican"

		reg `var' ib4.n_exp##c.cent_US* if country=="US" & n_exp != 1 & n_exp != 2 & democrat==1, robust
		eststo i_dem_`var'_US
		estadd local resp_sample "Democrat"
		
		reg `var' ib4.n_exp##c.cent_US* if country=="US" & n_exp != 1 & n_exp != 2 & other==1, robust
		eststo i_oth_`var'_US
		estadd local resp_sample "Independent/Other"
		
}


* store results

esttab i_* using "immigration_results_expandedparty_us.csv", star(* 0.10 ** 0.05 *** 0.01) b(2) t(2) p(2) nogaps nobaselevels noconstant label depvars scalar(resp_sample) replace


* coefficient plots and combine

foreach var of local dvs {

	coefplot i_`var'_US i_gop_`var'_US i_dem_`var'_US i_oth_`var'_US, keep(3.n_exp) name(i_`var', replace) ///
	xline(0) ciopts(lwidth(medium)) title("`: var label `var''") scheme(tpepbw) ///
	p1(label(All Respondents) msymbol(D)) p2(label("Republicans") msymbol(O)) p3(label("Democrats") msymbol(T)) p4(label("Others/Independents") msymbol(Dh)) ///
	coeflabels(3.n_exp = " ") 
	
}
grc1leg behavior i_gc_alwaysvote i_gc_keepwatch i_gc_protest i_gc_understandpol  i_gc_associations ///
	beliefs i_gc_others i_gc_frienddiff i_gc_havepatience i_gc_diversity i_gc_helppeople ///
	belonging i_gc_obeylaws    i_gc_supportgov   i_gc_feelX i_gc_speakX, xcommon title("Immigration Treatment" "By Partisanship, US") rows(3) legendfrom(i_gc_alwaysvote)
graph export figure2.png, as(png) replace

restore



estimates drop _all

local dvs "gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelX gc_speakX"

preserve 


* run regressions

foreach var of local dvs {

		
		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2, robust
		eststo i_`var'_UK
		estadd local resp_sample "All"

		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2 & tory==1, robust
		eststo i_tory_`var'_UK
		estadd local resp_sample "Conservative"

		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2 & labour==1, robust
		eststo i_lab_`var'_UK
		estadd local resp_sample "Labour"
		
		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2 & ukip==1, robust
		eststo i_ukip_`var'_UK
		estadd local resp_sample "UKIP"
		
		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2 & none==1, robust
		eststo i_oth_`var'_UK
		estadd local resp_sample "Independent/Other"
		
}


* store results

esttab i_* using "immigration_results_expandedparty_uk.csv", star(* 0.10 ** 0.05 *** 0.01) b(2) t(2) p(2) nogaps nobaselevels noconstant label depvars scalar(resp_sample) replace


* coefficient plots and combine

foreach var of local dvs {

	coefplot i_`var'_UK i_tory_`var'_UK i_lab_`var'_UK i_ukip_`var'_UK i_oth_`var'_UK, keep(3.n_exp) name(i_`var', replace) ///
	xline(0) ciopts(lwidth(medium)) title("`: var label `var''") scheme(tpepbw) ///
	p1(label(All Respondents) msymbol(D)) p2(label("Conservative") msymbol(O)) p3(label("Labour") msymbol(T)) ///
	p4(label("UKIP") msymbol(Dh)) p5(label("Others/Independents") msymbol(Oh)) coeflabels(3.n_exp = " ") 
	
}
grc1leg behavior i_gc_alwaysvote i_gc_keepwatch i_gc_protest i_gc_understandpol  i_gc_associations ///
	beliefs i_gc_others i_gc_frienddiff i_gc_havepatience i_gc_diversity i_gc_helppeople ///
	belonging i_gc_obeylaws    i_gc_supportgov   i_gc_feelX i_gc_speakX, xcommon title("Immigration Treatment" "By Partisanship, UK") rows(3) legendfrom(i_gc_alwaysvote)
graph export figure3.png, as(png) replace

restore




estimates drop _all

local dvs "gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelX gc_speakX"

preserve 
gen party_de = party if country=="DE"
replace party_de = 10 if (party==4 | party==5 | party>7) & country=="DE"


* run regressions

foreach var of local dvs {

		
		reg `var' ib4.n_exp##c.cent_DE* if country=="DE" & n_exp != 1 & n_exp != 2, robust
		eststo i_`var'_DE
		estadd local resp_sample "All"

		reg `var' ib4.n_exp##c.cent_DE* if country=="DE" & n_exp != 1 & n_exp != 2 & party_de==2, robust
		eststo i_cdu_`var'_DE
		estadd local resp_sample "CDU/CSU"

		reg `var' ib4.n_exp##c.cent_DE* if country=="DE" & n_exp != 1 & n_exp != 2 & party_de==3, robust
		eststo i_spd_`var'_DE
		estadd local resp_sample "SPD"
		
		reg `var' ib4.n_exp##c.cent_DE* if country=="DE" & n_exp != 1 & n_exp != 2 & party_de==6, robust
		eststo i_gru_`var'_DE
		estadd local resp_sample "Greens"

		reg `var' ib4.n_exp##c.cent_DE* if country=="DE" & n_exp != 1 & n_exp != 2 & party_de==7, robust
		eststo i_afd_`var'_DE
		estadd local resp_sample "AfD"
		
		reg `var' ib4.n_exp##c.cent_DE* if country=="DE" & n_exp != 1 & n_exp != 2 & party_de==10, robust
		eststo i_oth_`var'_DE
		estadd local resp_sample "All Others"
		
}


* store results

esttab i_* using "immigration_results_expandedparty_de.csv", star(* 0.10 ** 0.05 *** 0.01) b(2) t(2) p(2) nogaps nobaselevels noconstant label depvars scalar(resp_sample) replace


* coefficient plots and combine

foreach var of local dvs {

	coefplot i_`var'_DE i_cdu_`var'_DE i_spd_`var'_DE i_gru_`var'_DE i_afd_`var'_DE i_oth_`var'_DE, keep(3.n_exp) name(i_`var', replace) ///
	xline(0) ciopts(lwidth(medium)) title("`: var label `var''") scheme(tpepbw) ///
	p1(label(All Respondents) msymbol(D)) p2(label("CDU/CSU") msymbol(O)) p3(label("SPD") msymbol(T)) p4(label("Greens") msymbol(Dh)) ///
	p5(label("AfD") msymbol(Oh)) p6(label("Others") msymbol(Th)) coeflabels(3.n_exp = " ") 
	
}
grc1leg behavior i_gc_alwaysvote i_gc_keepwatch i_gc_protest i_gc_understandpol  i_gc_associations ///
	beliefs i_gc_others i_gc_frienddiff i_gc_havepatience i_gc_diversity i_gc_helppeople ///
	belonging i_gc_obeylaws    i_gc_supportgov   i_gc_feelX i_gc_speakX, xcommon title("Immigration Treatment" "By Partisanshp, Germany") rows(3) legendfrom(i_gc_alwaysvote)
graph export figure4.png, as(png) replace

restore



************************
* By Brexit, for the UK
************************

estimates drop _all

local dvs "gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelX gc_speakX"


* run regressions

foreach var of local dvs {
		
		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2, robust
		eststo i_`var'_UK
		estadd local resp_sample "All"
				
		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2 & brexit==1, robust
		eststo i_le_`var'_UK
		estadd local resp_sample "Leave"

		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2 & remain==1, robust
		eststo i_re_`var'_UK
		estadd local resp_sample "Remain"
		
		reg `var' ib4.n_exp##c.cent_UK* if country=="UK" & n_exp != 1 & n_exp != 2 & abstain==1, robust
		eststo i_ab_`var'_UK
		estadd local resp_sample "Abstain"
		
}


* store results

esttab i_* using "immigration_results_bybrexit.csv", star(* 0.10 ** 0.05 *** 0.01) b(2) t(2) p(2) nogaps nobaselevels noconstant label depvars scalar(resp_sample) replace


* coefficient plots and combine

foreach var of local dvs {

	coefplot i_`var'_UK i_le_`var'_UK i_re_`var'_UK i_ab_`var'_UK, keep(3.n_exp) name(i_`var', replace) xline(0) ciopts(lwidth(medium)) title("`: var label `var''") scheme(tpepbw) ///
		p1(label(All Respondents) msymbol(D)) p2(label(Leave Voters) msymbol(O)) p3(label(Remain Voters) msymbol(T)) p4(label(Abstainers) msymbol(Dh)) legend(rows(1)) coeflabels(3.n_exp = " ") 
	
}
grc1leg behavior i_gc_alwaysvote i_gc_keepwatch i_gc_protest i_gc_understandpol  i_gc_associations ///
	beliefs i_gc_others i_gc_frienddiff i_gc_havepatience i_gc_diversity i_gc_helppeople ///
	belonging i_gc_obeylaws    i_gc_supportgov   i_gc_feelX i_gc_speakX, xcommon title("Immigration Treatment" "by Brexit Support, UK") rows(3) legendfrom(i_gc_alwaysvote)
graph export appendixe.png, as(png) replace




************************
* OBJECTION ANALYSES
************************

* US

preserve

use "working_data_us.dta", clear 
merge 1:1 caseid using sentiment_data_us.dta, nogen

replace Objection2 = 1 if Objection2 == 10
egen nmiss = rowmiss(Objection*) 
egen objection = rowtotal(Objection*)
replace objection = . if nmiss==13
tab objection
gen objection_agree = objection
recode objection_agree (1 = 0) (2 = 1) (3 = 1)
label var objection_agree "Object to Vignette"
gen objection_coding = objection
recode objection_coding (0 = 1) (1 = 0) (2 = 0) (3 = 1)

drop nmiss
egen nmiss = rowmiss(Attention*) 
egen attention = rowtotal(Attention*)
tab attention
gen attention_agree = attention
recode attention_agree (1 = 0) (2 = 1) (3 = 1)
gen attention_coding = attention
recode attention_coding (0 = 1) (1 = 0) (2 = 0) (3 = 1)

drop nmiss
egen nmiss = rowmiss(Emotion*) 
egen emotion = rowtotal(Emotion*)
tab emotion
gen emotion_agree = emotion
recode emotion_agree (1 = 0) (2 = 1) (3 = 1)
gen emotion_coding = emotion
recode emotion_coding (0 = 1) (1 = 0) (2 = 0) (3 = 1)

gen objection_emotion = 0 if objection_agree==1
replace objection_emotion = 1 if objection_agree==1 & emotion_agree==1


* create coding reports

putdocx begin
putdocx paragraph
putdocx text ("Intercoder Reliability Report"), bold

putdocx paragraph
putdocx text ("Intercoder reliability is relatively straightforward to calculate. Because there are only three coders and each variable has only two values")
putdocx text (", the only options are all code yes (1,1,1), all code no (0,0,0), or two code yes and one no or two code no and one yes, or (1,1,0) or (1,0,0) ")
putdocx text ("in some order. The first and second correspond to agreement, and the third and fourth to some disagreement; there are, happily, no other kinds ")
putdocx text ("of disagreement possible. Reliability can therefore be measured with just the fraction of cases that are coded in agreement. A formal test of ")
putdocx text ("intercoder reliability can be represented with Fleiss's kappa statistic.")

putdocx paragraph
qui sum attention_coding
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
kap Attention*
local kappa : display %4.3f `r(kappa)'
putdocx text ("For Attention, all three coders agreed in `c2' percent of the cases. Kappa = `kappa'.")

putdocx paragraph
qui sum objection_coding
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
kap Objection*
local kappa : display %4.3f `r(kappa)'
putdocx text ("For Objection, all three coders agreed in `c2' percent of the cases. Kappa = `kappa'.")

putdocx paragraph
qui sum emotion_coding
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
kap Emotion*
local kappa : display %4.3f `r(kappa)'
putdocx text ("For Emotion, all three coders agreed in `c2' percent of the cases. Kappa = `kappa'.")

putdocx paragraph
qui sum objection_emotion if objection_agree==1
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
local objecters : display `r(N)'
qui sum objection_emotion if objection_emotion==1
local emotional_objectors : display `r(N)'

putdocx text ("Adopting the coding that someone is emotional if at least 2/3 coders coded it so, and that s/he objected if at least 2/3 coders ")
putdocx text ("coded it as so, `c2' percent of those who objected (`emotional_objectors' out of `objecters') were also emotional.")


putdocx paragraph
putdocx text ("This table gives you objection rates by partyid.")
putdocxcrosstab pid3 objection_agree , row

putdocx paragraph
putdocx text ("This table gives you party rates by objection.")
putdocxcrosstab  objection_agree pid3, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment.")
putdocxcrosstab n_exp objection_agree, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment FOR REPUBLICANS ONLY.")
putdocxcrosstab n_exp objection_agree if republican==1, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment FOR NOT REPUBLICANS.")
putdocxcrosstab n_exp objection_agree if republican==0, row

putdocx save "coding_report_us.docx", replace



* prep for analysis

label var gc_alwaysvote "Always Votes"
label var gc_obeylaws "Obeys the Law"
label var gc_keepwatch "Watches the Gov't"
label var gc_associations "Joins Associations"
label var gc_helppeople "Helps People"
label var gc_others "Understands Others"
label var gc_frienddiff "Different Friends"
label var gc_havepatience "Is Patient"
label var gc_supportgov "Support Govt"
label var gc_protest "Protests"
label var gc_understandpol "Understands Politics"
label var gc_diversity "Supports Diversity"
label var gc_feelamerican "Feels American"
label var gc_speakenglish "Speaks English"

local ivs = "female age college objection_agree"
tabulate region, generate(US_rdummy)

unab regions : `ivs' US_rdummy*

foreach tocenter of local regions {
	summarize `tocenter'
	generate cent_US_`tocenter' = `tocenter' - r(mean)
}

tab objection_agree

estimates drop _all
graph drop _all

twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "Behavior", size(vhuge)) name(behavior, replace)
twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "Liberal" "Values", size(vhuge)) name(beliefs, replace)
twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "National" "Belonging", size(vhuge)) name(belonging, replace)


* run regressions

local dvs "gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelamerican gc_speakenglish"

foreach var of local dvs {

	reg `var' ib4.n_exp##c.cent_US* if n_exp != 2 & n_exp != 1, robust
	eststo i_`var'_US
	
	reg `var' ib4.n_exp##c.cent_US* if n_exp != 2 & n_exp != 1 & objection_agree==0, robust
	eststo i_nob_`var'_US
	
	reg `var' ib4.n_exp##c.cent_US* if n_exp != 2 & n_exp != 1 & objection_agree==1, robust
	eststo i_ob_`var'_US
	
}


* coefficient plots and combine

foreach var of local dvs {

	coefplot i_`var'_US i_nob_`var'_US i_ob_`var'_US, keep(3.n_exp) name(i_`var', replace) xline(0) ciopts(lwidth(medium)) title("`: var label `var''") scheme(tpepbw) ///
		p1(label(All Respondents) msymbol(D) color(black)) p2(label("Non-Objectors") msymbol(O) color(gs10)) p3(label("Objectors") msymbol(T) color(gs8)) legend(rows(1)) coeflabels(3.n_exp = " ") 
	
}

grc1leg behavior i_gc_alwaysvote i_gc_keepwatch i_gc_associations i_gc_protest  i_gc_understandpol   ///
	beliefs  i_gc_helppeople i_gc_others i_gc_frienddiff i_gc_havepatience i_gc_diversity  ///
	belonging i_gc_obeylaws    i_gc_supportgov   i_gc_feelamerican i_gc_speakenglish, xcommon title("Immigration Treatment" "by Objection, US") rows(3) legendfrom(i_gc_alwaysvote)
graph export figure5.png, as(png) replace


restore


* UK

preserve

use "working_data_uk.dta", clear
merge 1:1 PERSISTENT_ID using sentiment_data_uk.dta, nogen

egen nmiss = rowmiss(Objection*) 
egen objection = rowtotal(Objection*)
replace objection = . if nmiss!=4
tab objection
gen objection_agree = objection
recode objection_agree (1 = 0) (2 = 1) (3 = 1)
label var objection_agree "Object to Vignette"
gen objection_coding = objection
recode objection_coding (0 = 1) (1 = 0) (2 = 0) (3 = 1)


drop nmiss
egen nmiss = rowmiss(Emotion*) 
egen emotion = rowtotal(Emotion*)
replace emotion = . if nmiss!=4
tab emotion
gen emotion_agree = emotion
recode emotion_agree (1 = 0) (2 = 1) (3 = 1)
gen emotion_coding = emotion
recode emotion_coding (0 = 1) (1 = 0) (2 = 0) (3 = 1)

gen objection_emotion = 0 if objection_agree==1
replace objection_emotion = 1 if objection_agree==1 & emotion_agree==1


* create coding reports

putdocx begin
putdocx paragraph
putdocx text ("Intercoder Reliability Report"), bold

putdocx paragraph
putdocx text ("Intercoder reliability is relatively straightforward to calculate. Because there are only three coders and each variable has only two values")
putdocx text (", the only options are all code yes (1,1,1), all code no (0,0,0), or two code yes and one no or two code no and one yes, or (1,1,0) or (1,0,0) ")
putdocx text ("in some order. The first and second correspond to agreement, and the third and fourth to some disagreement; there are, happily, no other kinds ")
putdocx text ("of disagreement possible. Reliability can therefore be measured with just the fraction of cases that are coded in agreement. A formal test of ")
putdocx text ("intercoder reliability can be represented with Fleiss's kappa statistic.")

putdocx paragraph
qui sum objection_coding
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
kap Objection*
local kappa : display %4.3f `r(kappa)'
putdocx text ("For Objection, all three coders agreed in `c2' percent of the cases. Kappa = `kappa'.")

putdocx paragraph
qui sum emotion_coding
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
kap Emotion*
local kappa : display %4.3f `r(kappa)'
putdocx text ("For Emotion, all three coders agreed in `c2' percent of the cases. Kappa = `kappa'.")

putdocx paragraph
qui sum objection_emotion if objection_agree==1
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
local objecters : display `r(N)'
qui sum objection_emotion if objection_emotion==1
local emotional_objectors : display `r(N)'

putdocx text ("Adopting the coding that someone is emotional if at least 2/3 coders coded it so, and that s/he objected if at least 2/3 coders ")
putdocx text ("coded it as so, `c2' percent of those who objected (`emotional_objectors' out of `objecters') were also emotional.")

putdocx paragraph
putdocx text ("This table gives you objection rates by Brexit vote.")
putdocxcrosstab v_101 objection_agree , row

putdocx paragraph
putdocx text ("This table gives you Brexit vote rates by objection.")
putdocxcrosstab  objection_agree v_101, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment.")
putdocxcrosstab n_exp objection_agree, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment FOR LEAVERS ONLY.")
putdocxcrosstab n_exp objection_agree if v_101==2, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment FOR NOT LEAVERS.")
putdocxcrosstab n_exp objection_agree if v_101!=2, row


putdocx save "coding_report_uk.docx", replace



* prep for analysis

label var gc_alwaysvote "Always Votes"
label var gc_obeylaws "Obeys the Law"
label var gc_keepwatch "Watches the Gov't"
label var gc_associations "Joins Associations"
label var gc_helppeople "Helps People"
label var gc_others "Understands Others"
label var gc_frienddiff "Different Friends"
label var gc_havepatience "Is Patient"
label var gc_supportgov "Support Govt"
label var gc_protest "Protests"
label var gc_understandpol "Understands Politics"
label var gc_diversity "Supports Diversity"
label var gc_feelamerican "Feels British"
label var gc_speakenglish "Speaks English"



twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "Behavior", size(vhuge)) name(behavior, replace)
twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "Liberal" "Values", size(vhuge)) name(beliefs, replace)
twoway (scatteri 1 0, msymbol(none)), yscale(noline) xscale(noline) ytitle("") xtitle("") ylabel(none) xlabel(none) text(1 0 "National" "Belonging", size(vhuge)) name(belonging, replace)


local ivs = "female age college objection_agree"
tabulate region, generate(UK_rdummy)

unab regions : `ivs' UK_rdummy*
	
foreach tocenter of local regions {
	summarize `tocenter'
	generate cent_UK_`tocenter' = `tocenter' - r(mean)
}


* run regressions

local dvs "gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelamerican gc_speakenglish"

foreach var of local dvs {

	reg `var' ib4.n_exp##c.cent_UK* if n_exp != 2 & n_exp != 1, robust
	eststo i_`var'_UK
	
	reg `var' ib4.n_exp##c.cent_UK* if n_exp != 2 & n_exp != 1 & objection==0, robust
	eststo i_no_`var'_UK
	
	reg `var' ib4.n_exp##c.cent_UK* if n_exp != 2 & n_exp != 1 & objection==1, robust
	eststo i_o_`var'_UK
	
}


* coefficient plots and combine

foreach var of local dvs {

	coefplot i_`var'_UK i_no_`var'_UK i_o_`var'_UK, keep(3.n_exp) name(i_`var', replace) xline(0) ciopts(lwidth(medium))  title("`: var label `var''") scheme(tpepbw) ///
		p1(label(All Respondents) msymbol(D) color(black)) p2(label("Non-Objectors") msymbol(O) color(gs10)) p3(label("Objectors") msymbol(T) color(gs8)) legend(rows(1)) coeflabels(3.n_exp = " ") 
	
}


grc1leg behavior i_gc_alwaysvote i_gc_keepwatch i_gc_protest i_gc_understandpol  i_gc_associations ///
	beliefs i_gc_others i_gc_frienddiff i_gc_havepatience i_gc_diversity i_gc_helppeople ///
	belonging i_gc_obeylaws  i_gc_supportgov  i_gc_feelamerican i_gc_speakenglish, ///
	ycommon xcommon title("Immigration Threat Treatment" "By Objection, UK") rows(3) legendfrom(i_gc_alwaysvote)
graph export "appendixf.png", as(png) replace

restore




* UK

preserve 

use "working_data_de.dta", clear
merge 1:1 PERSISTENT_ID using sentiment_data_de.dta, nogen

egen nmiss = rowmiss(Objection*) 
egen objection = rowtotal(Objection*)
tab objection
gen objection_agree = objection
recode objection_agree (1 = 0) (2 = 1) (3 = 1)
label var objection_agree "Object to Vignette"
gen objection_coding = objection
recode objection_coding (0 = 1) (1 = 0) (2 = 0) (3 = 1)

drop nmiss
egen nmiss = rowmiss(Emotion*) 
egen emotion = rowtotal(Emotion*)
tab emotion
gen emotion_agree = emotion
recode emotion_agree (1 = 0) (2 = 1) (3 = 1)
gen emotion_coding = emotion
recode emotion_coding (0 = 1) (1 = 0) (2 = 0) (3 = 1)

gen objection_emotion = 0 if objection_agree==1
replace objection_emotion = 1 if objection_agree==1 & emotion_agree==1


* create coding report

putdocx begin
putdocx paragraph
putdocx text ("Intercoder Reliability Report"), bold

putdocx paragraph
putdocx text ("Intercoder reliability is relatively straightforward to calculate. Because there are only three coders and each variable has only two values")
putdocx text (", the only options are all code yes (1,1,1), all code no (0,0,0), or two code yes and one no or two code no and one yes, or (1,1,0) or (1,0,0) ")
putdocx text ("in some order. The first and second correspond to agreement, and the third and fourth to some disagreement; there are, happily, no other kinds ")
putdocx text ("of disagreement possible. Reliability can therefore be measured with just the fraction of cases that are coded in agreement. A formal test of ")
putdocx text ("intercoder reliability can be represented with Fleiss's kappa statistic.")

putdocx paragraph
qui sum objection_coding
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
kap Objection*
local kappa : display %4.3f `r(kappa)'
putdocx text ("For Objection, all three coders agreed in `c2' percent of the cases. Kappa = `kappa'.")

putdocx paragraph
qui sum emotion_coding
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
kap Emotion*
local kappa : display %4.3f `r(kappa)'
putdocx text ("For Emotion, all three coders agreed in `c2' percent of the cases. Kappa = `kappa'.")

putdocx paragraph
qui sum objection_emotion if objection_agree==1
local concordance : display %4.3f `r(mean)'
local c2 = `concordance'*100
local objecters : display `r(N)'
qui sum objection_emotion if objection_emotion==1
local emotional_objectors : display `r(N)'

putdocx text ("Adopting the coding that someone is emotional if at least 2/3 coders coded it so, and that s/he objected if at least 2/3 coders ")
putdocx text ("coded it as so, `c2' percent of those who objected (`emotional_objectors' out of `objecters') were also emotional.")


gen ingovt = (party == 2 | party == 3)

putdocx paragraph
putdocx text ("This table gives you objection rates by party affiliation.")
putdocxcrosstab party objection_agree , row

putdocx paragraph
putdocx text ("This table gives you party affiliation rates by objection.")
putdocxcrosstab  objection_agree party, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment.")
putdocxcrosstab n_exp objection_agree, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment FOR CDU/CSU & SPD ONLY.")
putdocxcrosstab n_exp objection_agree if ingovt==1, row

putdocx paragraph
putdocx text ("This table gives you objection rates by treatment FOR NOT CDU/CSU OR SPD.")
putdocxcrosstab n_exp objection_agree if ingovt!=1, row


putdocx save "coding_report_de.docx", replace


* prep for analysis

label var gc_alwaysvote "Always Votes"
label var gc_obeylaws "Obeys the Law"
label var gc_keepwatch "Watches the Gov't"
label var gc_associations "Joins Associations"
label var gc_helppeople "Helps People"
label var gc_others "Understands Others"
label var gc_frienddiff "Different Friends"
label var gc_havepatience "Is Patient"
label var gc_supportgov "Support Govt"
label var gc_protest "Protests"
label var gc_understandpol "Understands Politics"
label var gc_diversity "Supports Diversity"
label var gc_feelamerican "Feels German"
label var gc_speakenglish "Speaks German"

local ivs = "female age college objection_agree"
tabulate region, generate(DE_rdummy)

unab regions : `ivs' DE_rdummy*
	
foreach tocenter of local regions {
	summarize `tocenter'
	generate cent_DE_`tocenter' = `tocenter' - r(mean)
}


* run regressions

local dvs "gc_alwaysvote gc_obeylaws gc_keepwatch gc_associations gc_helppeople gc_others gc_frienddiff gc_havepatience gc_supportgov gc_protest gc_understandpol gc_diversity gc_feelamerican gc_speakenglish"

foreach var of local dvs {

	reg `var' ib4.n_exp##c.cent_DE* if n_exp != 2 & n_exp != 1, robust
	eststo i_`var'_DE
	
	reg `var' ib4.n_exp##c.cent_DE* if n_exp != 2 & n_exp != 1 & objection==0, robust
	eststo i_no_`var'_DE
	
	reg `var' ib4.n_exp##c.cent_DE* if n_exp != 2 & n_exp != 1 & objection==1, robust
	eststo i_o_`var'_DE
	
}

foreach var of local dvs {

	coefplot i_`var'_DE i_no_`var'_DE i_o_`var'_DE, keep(3.n_exp) name(i_`var', replace) xline(0) ciopts(lwidth(medium))  title("`: var label `var''") scheme(tpepbw) ///
		p1(label(All Respondents) msymbol(D) color(black)) p2(label("Non-Objectors") msymbol(O) color(gs10)) p3(label("Objectors") msymbol(T) color(gs8)) legend(rows(1)) coeflabels(3.n_exp = " ") 
	
}


* coefficient plots and combine

grc1leg behavior i_gc_alwaysvote i_gc_keepwatch i_gc_protest i_gc_understandpol  i_gc_associations ///
	beliefs i_gc_others i_gc_frienddiff i_gc_havepatience i_gc_diversity i_gc_helppeople ///
	belonging i_gc_obeylaws  i_gc_supportgov  i_gc_feelamerican i_gc_speakenglish, ///
	ycommon xcommon title("Immigration Threat Treatment" "By Objection, Germany") rows(3) legendfrom(i_gc_alwaysvote)
graph export "appendixg.png", as(png) replace

restore


* clean up 
rm combined.dta
